package com.codecrane.core.controller;

import com.codecrane.core.util.DateUtils;

import com.codecrane.core.web.Ajax;
import com.codecrane.core.web.AjaxReturn;
import com.sea.data.entity.Order;
import com.sea.data.service.OrderService;
import com.sea.physical.service.CreatePlanService;
import com.sea.physical.service.StockCreatePlanService;

import com.sea.product.service.NewProductRecordService;
import com.sea.product.service.NewProductService;
import com.sea.supply.entity.Produce;
import com.sea.supply.service.BuyService;
import com.sea.supply.service.ProduceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.util.*;

/**
 * 仪表盘主页控制器
 * Created by crane on 16/8/15.
 */
@Controller
@RequestMapping("/dashboard")
public class DashboardController {


    @Autowired
    private OrderService orderService;
    @Autowired
    private ProduceService produceService;
    @Autowired
    private NewProductService newProductService;
    @Autowired
    private NewProductRecordService newProductRecordService;

    @Autowired
    private BuyService buyService;
    @Autowired
    private CreatePlanService createPlanService;
    @Autowired
    private StockCreatePlanService stockCreatePlanService;

    /**
     * 仪表盘主页面
     *
     * @param model
     * @return
     */
    @RequestMapping(value = {"/", ""}, method = RequestMethod.GET)
    public String dashboardIndexPage(ModelMap model) {
        return "/power/dashboard";
    }

    @RequestMapping("/getStatusNumber")
    @ResponseBody
    public AjaxReturn getOrderStatusNumber() {
        AjaxReturn ajaxReturn = Ajax.ok();
        Map<String, Object> orderMap = new HashMap<>();
        Map<String, Object> produceMap = new HashMap<>();
        Map<String, Object> buyMap = new HashMap<>();
        List<Map<String, Object>> maps = new ArrayList<>();
        List<Order> orderNumber = orderService.findStatusNumber(0);
        List<Produce> produceNumber = produceService.findStatusNumber(0);
        Integer buyTotal = buyService.findBuyTotal();
        produceMap.put("produce", produceNumber);
        orderMap.put("order", orderNumber);
        buyMap.put("buy",buyTotal);

        maps.add(produceMap);
        maps.add(orderMap);
        maps.add(buyMap);
        ajaxReturn.setData(maps);
        return ajaxReturn;
    }

    @RequestMapping("/getNewProductNumber")
    @ResponseBody
    public AjaxReturn getNewProductNumber() {
        AjaxReturn ajaxReturn = Ajax.ok();
        List<Map<String, Object>> newProductNumber = newProductService.findNewProductNumber();
        System.out.println(newProductNumber);
        ajaxReturn.setData(newProductService.findNewProductNumber());
        return ajaxReturn;
    }

    @RequestMapping(value = "/getLogisticsStatus")
    @ResponseBody
    public AjaxReturn logisticsStatus(){
        AjaxReturn result =  Ajax.fail().setMsg("获取数据失败");
        result.setOk(true).setMsg("").setData(createPlanService.logisticsStatus());
        return result;
    }

    @RequestMapping(value = "/getWareLogisticsStatus")
    @ResponseBody
    public AjaxReturn getWareLogisticsStatus(){
        AjaxReturn result =  Ajax.fail().setMsg("获取数据失败");
        result.setOk(true).setMsg("").setData(stockCreatePlanService.logisticsStatus());
        return result;
    }

    @RequestMapping("/getNewProductRecord")
    @ResponseBody
    public AjaxReturn getNewProductRecord() throws ParseException {
        AjaxReturn result = Ajax.fail().setMsg("获取发起测评活动失败");
        List<HashMap<String, Object>> tList = new ArrayList<>();     // 结果集
        HashMap<String, Object> map = new HashMap<>();

        List<Integer> yj=new ArrayList<>();
        List<Integer> sj=new ArrayList<>();
        Date date = DateUtils.addMonth(new Date(), 6);
        //System.out.println(DateUtils.dateToString(date,"yyyy-MM"));
        Date date1 = DateUtils.addMonth(new Date(), -6);
        //System.out.println(DateUtils.dateToString(date1,"yyyy-MM"));
        List<String> halfYear = DateUtils.getMonthsBetweenDates(DateUtils.dateToString(date1, "yyyy-MM"), DateUtils.dateToString(date, "yyyy-MM"));


        for(String month:halfYear){
            yj.add(newProductRecordService.findByMonth(7,month));
            sj.add(newProductRecordService.findCountByProductDate(7,month));
        }
        map.put("date",halfYear);
        HashMap<String, Object> map1 = new HashMap<>();
        map1.put("yj",yj);
        HashMap<String, Object> map2 = new HashMap<>();
        map2.put("sj",sj);
        tList.add(map);
        tList.add(map1);
        tList.add(map2);
        result.setOk(true).setMsg("").setData(tList);
        return result;
    }

    public static void main(String[] args) throws ParseException {
        Date date = DateUtils.addMonth(new Date(), 6);
        //System.out.println(DateUtils.dateToString(date,"yyyy-MM"));
        Date date1 = DateUtils.addMonth(new Date(), -6);
        //System.out.println(DateUtils.dateToString(date1,"yyyy-MM"));
         DateUtils.getMonthsBetweenDates(DateUtils.dateToString(date1,"yyyy-MM"),DateUtils.dateToString(date,"yyyy-MM"));
    }
}

